/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package three2learn.Controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.Transaction;
import three2learn.Model.Course;
import three2learn.service.HibernateUtil;

/**
 *
 * @author Tim
 */
@WebServlet(urlPatterns = {"/course/nieuw", "/course/wijzig"})
public class CourseWijzigController extends HttpServlet {

    private static String titelNieuw = "Nieuwe Course";
    private static String titelWijzig = "Wijzigen Course";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        getFieldVars(request,response);
    }
    private void getFieldVars(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        if (request.getParameter("id") != null) {
            long id = Long.parseLong(request.getParameter("id"));
            request.setAttribute("id", id);

            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction tx = session.beginTransaction();
            Course course = (Course) session.get(Course.class, id);
            tx.commit();

            request.setAttribute("id", course.getId());
            request.setAttribute("name", course.getName());
            request.setAttribute("desc", course.getDescription());

            doorsturen(request, response, titelWijzig);
        } else {
            doorsturen(request, response, titelNieuw);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        boolean isUpdate = false;
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Course formCourse = getCourseFromRequest(request);
        if (formCourse.getId() > 0) {//als het een update is
            isUpdate = true;
        }
        if(isUpdate){
            session.update(formCourse);
        } else {
            session.save(formCourse);
        }
        tx.commit();//send to db

        getFieldVars(request,response);
    }

    private Course getCourseFromRequest(HttpServletRequest request) {
        Course c = new Course();

        if (request.getParameter("id") != null && !request.getParameter("id").isEmpty()) {
            c.setId(Long.parseLong(request.getParameter("id")));
        }
        if (request.getParameter("name") != null) {
            c.setName(request.getParameter("name"));
        }
        if (request.getParameter("desc") != null) {
            c.setDescription(request.getParameter("desc"));
        }
        return c;
    }

    private void doorsturen(HttpServletRequest request, HttpServletResponse response, String titel)
            throws IOException, ServletException {
        request.setAttribute("paginaTitel", titel);

        String address = "/createCourse.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
